<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-row>
        <el-col :span="8" style="text-align: left">
          <el-button class="filter-item" size="small" style="margin-left: 10px;" type="primary" v-waves
                   @click="cleanStockFunc">清理库存
          </el-button>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-input size="small" style="width: 200px" class="filter-item fsc-search" placeholder="车架号-回车查询"
                    v-model="listQuery.condition.vin" @keyup.enter.native="searchClick"></el-input>
          <el-button class="filter-item" size="small" type="primary" v-waves icon="el-icon-search" @click="searchClick">
            搜索
          </el-button>
          <el-button class="filter-item" size="small" type="primary" v-waves icon="el-icon-circle-plus-outline"
                     @click="moreClick">更多
          </el-button>
          <el-dropdown trigger="click" :hide-on-click=false style="float: right;margin-left: 10px">
            <span class="el-dropdown-link">
              <el-button class="filter-item" size="small" type="primary" v-waves>
                选择列
                <i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-checkbox-group v-model="theadCheckVal">
                  <el-checkbox label="putwaylineId">入库记录id</el-checkbox>
                  <el-checkbox label="putWayVin">车架号</el-checkbox>
                  <el-checkbox label="storeHouseId">仓库</el-checkbox>
                  <el-checkbox label="storeName">仓库名称</el-checkbox>
                  <el-checkbox label="locationId">库位</el-checkbox>
                  <el-checkbox label="stockId">库存id</el-checkbox>
                  <el-checkbox label="qty">库存量</el-checkbox>
                  <el-checkbox label="sourceKey">指令号</el-checkbox>
                  <el-checkbox label="releaseGid">系统运单号</el-checkbox>
                  <el-checkbox label="remarks">运单表备注</el-checkbox>
                </el-checkbox-group>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
      <div style="clean:both"></div>
    </div>
    <div v-if="searchFlag" style="margin-top: 10px;">
      <el-form ref="form" :model="listQuery.condition" label-width="80px" :inline="true">
        <el-form-item label="车架号:">
          <el-input type="textarea" :rows="2" style="width: 250px"
                    v-model="listQuery.condition.vin"  placeholder="请输入车架号, 以逗号来分隔"></el-input>
        </el-form-item>
        <el-form-item label="仓库名称:">
          <el-input v-model="listQuery.condition.storeName" placeholder="请输入仓库名称"></el-input>
        </el-form-item>
        <el-form-item label="入库时间:">
          <el-date-picker class="w200" v-model="listQuery.condition.startDate" type="datetime" placeholder="选择日期时间"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          align="right" ></el-date-picker>
          <span style="padding: 0 6px">至</span>
          <el-date-picker class="w200" v-model="listQuery.condition.endDate" type="datetime" placeholder="选择日期时间"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          align="right" ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="list" style="margin-left: 10px">查询</el-button>
          <el-button type="primary" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div style="clean:both"></div>

    <el-table ref="paymentTable" :data="paymentList" v-loading="listLoading"
              element-loading-text="加载中" border fit @selection-change="handleSelectionChange"
              highlight-current-row style="width: 100%;margin-top:5px">
      <el-table-column type="selection" width="35"></el-table-column>
      <el-table-column :key="key" align="center" :width="value.width" v-for='(value,key) in tableThead'
                       :label="value.title">
        <template slot-scope="scope">
          <div>{{scope.row[value.key]}}</div>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.current" :page-sizes="[10,20,30,50]" :page-size="listQuery.size"
                     layout="total, sizes, prev, pager, next, jumper" :total="listQuery.total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import * as inboundTaskAPI from '@/api/inbound/inboundTask'
import waves from '@/directive/waves' // 水波纹指令
import * as Utils from '@/utils/index'
const defaultTableThead = [
  { key: 'putwaylineId', title: '入库记录id', width: '' },
  { key: 'putWayVin', title: '车架号', width: 180 },
  { key: 'storeHouseId', title: '仓库', width: '' },
  { key: 'storeName', title: '仓库名称', width: '' },
  { key: 'locationId', title: '库位', width: 200 },
  { key: 'stockId', title: '库存id', width: '' },
  { key: 'qty', title: '库存量', width: '' },
  { key: 'sourceKey', title: '指令号', width: '' },
  { key: 'releaseGid', title: '系统运单号', width: '' },
  { key: 'remarks', title: '运单表备注', width: 150 }
]
export default {
  name: 'payment',
  directives: {
    waves
  },
  data() {
    return {
      theadCheckVal: defaultTableThead.map(item => item.key),
      tableThead: defaultTableThead,
      listQuery: {
        current: 1,
        size: 10,
        total: 0,
        condition: {
          startDate: Utils.parseTime(new Date().getTime(), '{y}-{m}-{d} 00:00:00'),
          endDate: Utils.parseTime(new Date().getTime(), '{y}-{m}-{d} 23:59:59')
        }
      },
      paymentList: [],
      multipleSelection: [],
      listLoading: true,
      searchFlag: true,
      downloadLoading: false,
      downloadData: []
    }
  },
  created() {
    this.list()
  },
  watch: {
    theadCheckVal(valArr) {
      this.tableThead = defaultTableThead.filter(v => {
        for (let i = 0; i < valArr.length; i++) {
          if (v.key === valArr[i]) {
            return v
          }
        }
      })
    }
  },
  methods: {
    list() {
      // 列表查询
      this.listLoading = true
      // 处理是否展开更多
      let queryParam = {}
      queryParam = this.listQuery
      // queryParam.condition.houseId = this.$store.getters.currentStorehouse.id
      inboundTaskAPI.queryCancleInbound(queryParam).then(result => {
        this.$data.listLoading = false
        if (result.code === 0) {
          this.$data.paymentList = result.data.records
          this.$data.listQuery.total = result.data.total
        } else {
          this.$notify({
            title: '信息提示',
            message: result.message,
            type: 'error',
            duration: 2000
          })
        }
      })
    },
    // 清除 入库队列
    cleanStockFunc() {
      const that = this
      if (that.checkSelect(that.multipleSelection)) {
        const ids = []
        that.multipleSelection.map(item => {
          ids.push(item.stockId)
        })
        const stockId = ids.length > 1 ? ids.join(',') : ids[0]
        inboundTaskAPI.cleanStock({ stockId }).then(result => {
          if (result.code === 0) {
            that.$message({
              message: '操作成功',
              type: 'success',
              duration: 2000
            })
            that.list()
          }
        }).catch(err => {
          console.log('error', err)
        })
      }
    },
    handleSizeChange(val) {
      // 分页size变化
      this.$data.listQuery.size = val
      this.list()
    },
    handleCurrentChange(val) {
      // 分页当前页变化
      this.$data.listQuery.current = val
      this.list()
    },
    moreClick() {
      // 更多click
      this.$data.searchFlag = !this.$data.searchFlag
    },
    searchClick() {
      // 搜索
      this.list()
    },
    // 检查是否选择单条
    checkSelect(data) {
      const l = data.length
      if (l === 0) {
        this.$message({
          message: '未选择',
          type: 'warning',
          duration: 2000
        })
        return false
      }
      return true
    },
    // 表格改变时
    handleSelectionChange(val) {
      const that = this
      that.multipleSelection = val
    },
    reset() {
      this.listQuery = {
        current: 1,
        size: 10,
        total: 0,
        condition: {}
      }
      this.list()
    }
  }
}
</script>
<style scoped>

</style>
